import { reactive } from 'vue'
import { defineStore } from 'pinia'
import wsClient from '@/ws/ws-client.js'
import { getInfo, getIncomes } from '@/api/user.js'

export const useUserStore = defineStore(
	'driver', 
	{
		state: () => ({
			info: {
				id: null,
				pid: null,
				profileImg: null,
				surname: null,
				name: null,
				sex: null,
				phone: null,
				idCard: null,
				idCardFrontImg: null,
				idCardBackImg: null,
				emergencyContact: null,
				emergencyContactPhone: null,
				driverType: null,
				monthlySubscriptionExpires: null,
				crownFrequency: null,
				crownTime: null,
				driverCardFrontImg: null,
				driverCardBackImg: null,
				driverCardReceiveTime: null,
				driverCardExpiresTime: null,
				driverAge: null,
				driverCarType: null,
				status: null,
				auditStatus: null,
				accountBalance: 0,
				withdrawingBalance: 0,
				bankName: null,
				bankCardId: null,
				isNoticeRead: true,
				vipType: 1,
				vipExpireTime: null,
				createTime: null
			},
			incomes: {
				todayIncome: 0,
				monthIncome: 0,
				totalIncome: 0,
				totalWithdraw: 0
			}
		}),
		actions: {
			getCurrentDriver () {
				return this.info
			},
			resetCurrentDriver () {
				wsClient.doNotNeedReconnectAnyMore()
				Object.keys(this.info).forEach(k => {
					this.info[k] = null
				})
			},
			refreshCurrentDriver () {
				return new Promise((resolve, reject) => {
					const task1 = getInfo()
					const task2 = getIncomes()
					Promise.all([ task1, task2 ]).then(res => {
						// 用户信息
						const userinfo = res[0].data
						// console.log(res[0].data)
						Object.assign(this.info, userinfo)
						// 收益信息
						const incomeinfo = res[1].data
						// console.log(res[1].data)
						Object.assign(this.incomes, incomeinfo)
						resolve()
					}).catch(err => {
						console.error('登录异常')
						reject(err)
					})
				})
			}
		},
		unistorage: true, // 开启后对 state 的数据读写都将持久化
	}
)